package com.tjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjx.entity.PackageDetail;
import com.tjx.entity.Ticket;
import com.tjx.mapper.PackageDetailMapper;
import com.tjx.mapper.TicketMapper;
import com.tjx.service.IPackageDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐详情表：仅保留自增主键，无其他索引和约束 服务实现类
 * </p>
 *
 * @author tjx
 * @since 2025-10-29
 */
@Service
public class PackageDetailServiceImpl extends ServiceImpl<PackageDetailMapper, PackageDetail> implements IPackageDetailService {
    @Autowired
    PackageDetailMapper packageDetailMapper;

    @Autowired
    TicketMapper ticketMapper;


    // 查询一个套餐详情服务
    @Override
    public List<Ticket> getPackageDetailByPackageId(PackageDetail packageDetail) {
        // 使用MyBatis-Plus的QueryWrapper构建查询条件
        QueryWrapper<PackageDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("package_id", packageDetail.getPackageId())
                .eq("is_deleted", 0); // 查询未删除的记录

        // 查询该套餐下的所有详情记录
        List<PackageDetail> packageDetails = packageDetailMapper.selectList(queryWrapper);
        if (packageDetails != null && !packageDetails.isEmpty()) {
            // 2. 提取所有票ID
            List<String> ticketIds = packageDetails.stream()
                    .map(PackageDetail::getTicketId)
                    .distinct()
                    .toList();

            if (ticketIds.isEmpty()) {
                return List.of();
            }
            QueryWrapper<Ticket> ticketQueryWrapper = new QueryWrapper<>();
            ticketQueryWrapper.in("ticket_id", ticketIds)
                    .eq("is_deleted", 0); // 查询未删除的记录

            // 3. 查询所有票信息
            return ticketMapper.selectList(ticketQueryWrapper); // 返回所有详情记录
        }
        return List.of();

    }
}
